<template>
  <div class="model-select">
    <el-form-item label="触发类型" prop="triggerType">
      <el-select v-model="modelForm.triggerType" placeholder="请选择触发类型" @change="handleTriggerTypeChange">
        <el-option label="属性" value="property" />
        <el-option label="功能" value="function" />
        <el-option label="事件" value="event" />
      </el-select>
    </el-form-item>

    <!-- 属性选择 -->
    <template v-if="modelForm.triggerType === 'property'">
      <el-form-item label="属性" prop="property">
        <el-select v-model="modelForm.property" placeholder="请选择属性" @change="handlePropertyChange">
          <el-option
            v-for="item in propertyList"
            :key="item.id"
            :label="item.name"
            :value="item.identifier"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item label="运算符" prop="operator">
        <el-select v-model="modelForm.operator" placeholder="请选择运算符">
          <el-option label="等于" value="eq" />
          <el-option label="不等于" value="neq" />
          <el-option label="大于" value="gt" />
          <el-option label="大于等于" value="gte" />
          <el-option label="小于" value="lt" />
          <el-option label="小于等于" value="lte" />
        </el-select>
      </el-form-item>

      <el-form-item label="数值" prop="value">
        <el-input v-model="modelForm.value" placeholder="请输入数值" />
      </el-form-item>
    </template>

    <!-- 功能选择 -->
    <template v-if="modelForm.triggerType === 'function'">
      <el-form-item label="功能" prop="function">
        <el-select v-model="modelForm.function" placeholder="请选择功能">
          <el-option
            v-for="item in functionList"
            :key="item.id"
            :label="item.name"
            :value="item.identifier"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item label="参数" prop="params" v-if="currentFunction && currentFunction.inputParams">
        <el-form :model="modelForm.params" label-width="80px">
          <el-form-item
            v-for="param in currentFunction.inputParams"
            :key="param.identifier"
            :label="param.name"
            :prop="param.identifier"
          >
            <el-input v-model="modelForm.params[param.identifier]" :placeholder="'请输入' + param.name" />
          </el-form-item>
        </el-form>
      </el-form-item>
    </template>

    <!-- 事件选择 -->
    <template v-if="modelForm.triggerType === 'event'">
      <el-form-item label="事件" prop="event">
        <el-select v-model="modelForm.event" placeholder="请选择事件">
          <el-option
            v-for="item in eventList"
            :key="item.id"
            :label="item.name"
            :value="item.identifier"
          />
        </el-select>
      </el-form-item>
    </template>
  </div>
</template>

<script>
import { getDeviceModel, getProductModel } from "@/api/iot/model";

export default {
  name: "ModelSelect",
  props: {
    deviceId: {
      type: [String, Number],
      default: ''
    },
    productId: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      modelForm: {
        triggerType: 'property',
        property: '',
        operator: '',
        value: '',
        function: '',
        params: {},
        event: ''
      },
      propertyList: [],
      functionList: [],
      eventList: [],
      currentFunction: null
    }
  },
  watch: {
    deviceId: {
      handler(newVal) {
        console.log('Device ID changed:', newVal);
        if (newVal) {
          this.getDeviceModel();
        }
      },
      immediate: true
    },
    productId: {
      handler(newVal) {
        console.log('Product ID changed:', newVal);
        if (newVal) {
          this.getProductModel();
        }
      },
      immediate: true
    }
  },
  methods: {
    // 获取设备物模型
    getDeviceModel() {
      if (!this.deviceId) return;
      getDeviceModel(this.deviceId).then(response => {
        if (response.code === 200) {
          const model = response.data;
          if (model) {
            this.propertyList = model.properties || [];
            this.functionList = model.functions || [];
            this.eventList = model.events || [];
          }
        }
      });
    },
    // 获取产品物模型
    getProductModel() {
      if (!this.productId) return;
      getProductModel(this.productId).then(response => {
        if (response.code === 200) {
          const model = response.data;
          if (model) {
            this.propertyList = model.properties || [];
            this.functionList = model.functions || [];
            this.eventList = model.events || [];
          }
        }
      });
    },
    // 处理触发类型变化
    handleTriggerTypeChange(val) {
      this.modelForm.property = '';
      this.modelForm.function = '';
      this.modelForm.event = '';
      this.modelForm.operator = '';
      this.modelForm.value = '';
      this.modelForm.params = {};
      this.$emit('change', this.modelForm);
    },
    // 处理属性变化
    handlePropertyChange(val) {
      this.modelForm.value = '';
      this.$emit('change', this.modelForm);
    },
    // 获取表单数据
    getData() {
      return this.modelForm;
    },
    // 重置表单
    reset() {
      this.modelForm = {
        triggerType: 'property',
        property: '',
        operator: '',
        value: '',
        function: '',
        params: {},
        event: ''
      };
    }
  }
}
</script>

<style scoped>
.model-select {
  margin-top: 15px;
}
</style> 